package com.wrb.points.service;

import com.wrb.points.custommapper.PointsCustomMapper;
import com.wrb.points.exception.ServiceException;
import com.wrb.points.mapper.PointsLogMapper;
import com.wrb.points.mapper.PointsMapper;
import com.wrb.points.model.Points;
import com.wrb.points.model.PointsCriteria;
import com.wrb.points.model.PointsLog;
import com.wrb.points.response.ResponseCode;
import com.wrb.points.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author wrb
 * @date 2019/12/3 11:29
 */
@Service
public class PointsService {

	@Autowired
	private PointsMapper pointsMapper;

	@Autowired
	private PointsCustomMapper pointsCustomMapper;

	@Autowired
	private PointsLogMapper pointsLogMapper;

	public Points getByCustomerId(long customerId) {
		PointsCriteria criteria = new PointsCriteria();
		criteria.or().andCustomerIdEqualTo(customerId);
		List<Points> pointsList = pointsMapper.selectByExample(criteria);
		if (CollectionUtils.isEmpty(pointsList)) {
			return null;
		}
		return pointsList.get(0);
	}

	public int insertPoints(Points points) {
		return pointsMapper.insertSelective(points);
	}

	/**
	 * 用户积分变动
	 *
	 * @param customerId
	 * @param transPoints
	 * @param transTitle
	 * @param transType
	 */
	@Transactional(rollbackFor = Exception.class)
	public void transPoints(long customerId, int transPoints, String transTitle, int transType) throws ServiceException {
		// 判断 transPoints 不能为 0 或负数
		if (transPoints <= 0) {
			throw new ServiceException(ResponseCode.TRANS_POINTS_ERROR, "积分变动数量不正确");
		}
		// 判断 transType 是否正确
		if (!(transType == 1 || transType == -1)) {
			throw new ServiceException(ResponseCode.TRANS_TYPE_ERROR, "积分变动流向类型不正确");
		}
		int amount = transPoints * transType;
		// 查询用户积分钱包
		Points points = this.getByCustomerId(customerId);
		if (points == null) {
			throw new ServiceException(ResponseCode.POINTS_USER_NOT_FOUND, "用户积分钱包找不到");
		}
		if (points.getAvailable() + amount < 0) {
			throw new ServiceException(ResponseCode.CUSTOMER_POINTS_NOT_ENOUGH, "用户积分不足");
		}
		// 修改用户积分钱包
		int result = this.pointsCustomMapper.pointsChange(points.getPointsId(), amount);
		if (result == 0) {
			throw new ServiceException(ResponseCode.DB_ERROR, "修改用户积分失败");
		}
		// 插入积分变更记录
		PointsLog pointsLog = new PointsLog();
		pointsLog.setPointsLogId(SnowflakeIdWorker.generateId());
		pointsLog.setPointsId(points.getPointsId());
		pointsLog.setCustomerId(customerId);
		pointsLog.setTransPoints(transPoints);
		pointsLog.setTransTitle(transTitle);
		pointsLog.setTransType(transType);
		pointsLog.setSourcePoints(points.getAvailable());
		pointsLog.setTargetPoints(points.getAvailable() + amount);
		int insertResult = pointsLogMapper.insertSelective(pointsLog);
		if (insertResult == 0) {
			throw new ServiceException(ResponseCode.DB_ERROR, "增加积分流水日志失败");
		}
	}
}
